# 绘制所有小鱼
import os
import random
from setting import *

import pygame


# 所有鱼的父类
class Fish(pygame.sprite.Sprite):
    def __init__(self, init_dict: dict) -> None:
        super().__init__()
        # 小鱼每一帧的图片
        self.img_list = []
        for each in os.listdir(init_dict['img_path']):
            self.img_list.append(pygame.image.load(os.path.join(init_dict['img_path'], each)).convert_alpha())
            # 默认以第一帧作为初始状态
        self.image = self.img_list[0]
        self.rect = self.image.get_rect()
        # 生命数量
        self.max_life = self.life = init_dict['max_life']
        # 被击杀后的得分
        self.kill_score = init_dict['kill_score']
        # 是否存活
        self.active = False
        # 图片替换索引
        self.img_index = 0
        # 初次出现的方向
        self.appear_direction = 'left'
        # 移动速度
        self.speed = init_dict['speed']
        # 标志位，判断碰撞使用
        self.mask = pygame.mask.from_surface(self.image)

    def reset(self):
        """ 初次设定/重新设定初始值 """
        self.active = True
        self.appear_direction = random.choice(['left', 'right', 'top'])
        self.life = self.max_life
        self.img_index = 0
        self.head_locate()
        self.relocate()

    """ 以下move系列方法，代表鱼向不同的方向移动 """

    def move_down(self):
        self.rect.top += self.speed
        if self.rect.top > HEIGHT:
            self.active = False

    def move_left(self):
        self.rect.left -= self.speed
        if self.rect.right < 0:
            self.active = False

    def move_right(self):
        self.rect.left += self.speed
        if self.rect.left > WIDTH:
            self.active = False

    def head_locate(self):
        """ 对头部进行重定位，如右边出来的鱼，头往左边，同时初始化坐标 """
        if self.appear_direction == 'right':
            self.image = pygame.transform.rotate(self.img_list[self.img_index], 180)
        elif self.appear_direction == 'top':
            self.image = pygame.transform.rotate(self.img_list[self.img_index], -90)
        else:
            self.image = self.img_list[self.img_index]

    def relocate(self):
        """ 对于初始位置重新定位 """
        if self.appear_direction == 'right':
            self.rect.top = random.randint(0, HEIGHT)
            self.rect.left = random.randint(WIDTH + self.rect.width + 200, WIDTH + self.rect.width + 400)
        elif self.appear_direction == 'top':
            self.rect.top = random.randint(-self.rect.width - 400, -self.rect.width - 200)
            self.rect.left = random.randint(0, WIDTH)
        else:
            self.rect.left = random.randint(-self.rect.width - 400, -self.rect.width - 200)
            self.rect.top = random.randint(0, HEIGHT)

    def update(self, delay: int):
        if delay % 2 == 0:
            self.img_index = (self.img_index + 1) % len(self.img_list)
            self.head_locate()
        if self.appear_direction == 'left':
            self.move_right()
        elif self.appear_direction == 'right':
            self.move_left()
        elif self.appear_direction == 'top':
            self.move_down()


# 小金鱼
class GoldFish(Fish):
    def __init__(self) -> None:
        init_dict = {
            'img_path': r'img/gold_fish',
            'max_life': 50,
            'speed': 3,
            'kill_score': 1000
        }
        super().__init__(init_dict)


# 章鱼
class OctopusFish(Fish):
    def __init__(self):
        init_dict = {
            'img_path': 'img/octopus_fish',
            'max_life': 100,
            'speed': 2,
            'kill_score': 1500
        }
        super().__init__(init_dict)

    def relocate(self):
        """ 对于初始位置重新定位 """
        if self.appear_direction == 'right':
            self.rect.top = random.randint(0, HEIGHT)
            self.rect.left = random.randint(WIDTH + self.rect.width + 300, WIDTH + self.rect.width + 600)
        elif self.appear_direction == 'top':
            self.rect.top = random.randint(-self.rect.width - 600, -self.rect.width - 300)
            self.rect.left = random.randint(0, WIDTH)
        else:
            self.rect.left = random.randint(-self.rect.width - 600, -self.rect.width - 300)
            self.rect.top = random.randint(0, HEIGHT)


# 金色魔鬼鱼
class MantaFish(Fish):
    def __init__(self) -> None:
        init_dict = {
            'img_path': 'img/manta_ray',
            'max_life': 150,
            'speed': 2,
            'kill_score': 3000
        }
        super().__init__(init_dict)

    def relocate(self):
        if self.appear_direction == 'right':
            self.rect.top = random.randint(0, HEIGHT)
            self.rect.left = random.randint(WIDTH + self.rect.width + 600, WIDTH + self.rect.width + 900)
        elif self.appear_direction == 'top':
            self.rect.top = random.randint(-self.rect.width - 900, -self.rect.width - 600)
            self.rect.left = random.randint(0, WIDTH)
        else:
            self.rect.left = random.randint(-self.rect.width - 900, -self.rect.width - 600)
            self.rect.top = random.randint(0, HEIGHT)


# 蓝色鲨鱼
class SharkFish(Fish):
    def __init__(self) -> None:
        init_dict = {
            'img_path': 'img/shark',
            'max_life': 250,
            'speed': 3,
            'kill_score': 5000
        }
        super().__init__(init_dict)

    def relocate(self):
        if self.appear_direction == 'right':
            self.rect.top = random.randint(0, HEIGHT)
            self.rect.left = random.randint(WIDTH + self.rect.width + 800, WIDTH + self.rect.width + 1300)
        elif self.appear_direction == 'top':
            self.rect.top = random.randint(-self.rect.width - 1300, -self.rect.width - 800)
            self.rect.left = random.randint(0, WIDTH)
        else:
            self.rect.left = random.randint(-self.rect.width - 1300, -self.rect.width - 800)
            self.rect.top = random.randint(0, HEIGHT)


# boss鱼
class BossFish(Fish):
    def __init__(self) -> None:
        init_dict = {
            'img_path': 'img/boss',
            'max_life': 400,
            'speed': 3,
            'kill_score': 10000
        }
        super().__init__(init_dict)

    def relocate(self):
        if self.appear_direction == 'right':
            self.rect.top = random.randint(0, HEIGHT)
            self.rect.left = random.randint(WIDTH + self.rect.width + 1500, WIDTH + self.rect.width + 2000)
        elif self.appear_direction == 'top':
            self.rect.top = random.randint(-self.rect.width - 2000, -self.rect.width - 1500)
            self.rect.left = random.randint(0, WIDTH)
        else:
            self.rect.left = random.randint(-self.rect.width - 2000, -self.rect.width - 1500)
            self.rect.top = random.randint(0, HEIGHT)
